//
//  GJOilTypeSelected.m
//  xmgjAppFront
//
//  Created by Zeki on 2020/7/11.
//  Copyright © 2020 guojutech. All rights reserved.
//


#import "GJOilTypeSelected.h"

@interface oilTypeTtem ()
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)GJOilClassificationModel *model;
@end
@implementation oilTypeTtem
- (instancetype)initWithModel:(GJOilClassificationModel *)model
{
  self = [super init];
  if (self) {
    [self addSubview:self.label];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
      make.edges.equalTo(self);
    }];
    
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
    self.label.text = model.name;
    self.model = model;
    if (model.isSelected) {
      self.backgroundColor = HexColor(#F1E9DB);
      self.label.textColor = HexColor(#BA914A);
    }else{
      self.backgroundColor = HexColor(#F5F4F2);
      self.label.textColor = HexColor(#999796);
    }
  }
  return self;
}

- (UILabel *)label{
  if (!_label) {
    _label = [UILabel new];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.font = kSystemFontOfSize(14);
    _label.adjustsFontSizeToFitWidth = YES;
  }
  return _label;
}
@end

@interface GJOilTypeSelected ()
@property (weak, nonatomic) IBOutlet UIView *IB_view;
@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, copy)void(^itemClickBlcok)(GJOilClassificationModel *model);
@end

@implementation GJOilTypeSelected


- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self = [[[NSBundle mainBundle] loadNibNamed:@"GJOilTypeSelected" owner:self options:nil] lastObject];
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.IB_view.layer.cornerRadius = 4;
    self.IB_view.layer.masksToBounds  = YES;
  }
  return self;
}
- (IBAction)IB_buttonAction:(UIButton *)sender {
  switch (sender.tag) {
    case 10:
    {
      //取消
      [self removeFromSuperview];
    }
      break;
    case 11:
    {
      //点击空白页面
      [self removeFromSuperview];
    }
      break;;
    default:
      break;
  }
}
- (void)didSelectedItem:(oilTypeTtem *)item{
  [self removeFromSuperview];
  self.itemClickBlcok(item.model);
}
- (void)setDataArray:(NSArray *)dataArray{
  _dataArray = dataArray;
  CGFloat itemWidth = (kScreenWidth - 8 * 3 - 16 * 2) / 4;
  CGFloat itemHeight = 32;
  for (int i = 0; i < dataArray.count; i ++) {
    oilTypeTtem *item = [[oilTypeTtem alloc]initWithModel:dataArray[i]];
    [item addTarget:self  action:@selector(didSelectedItem:) forControlEvents:UIControlEventTouchUpInside];
    [self.IB_view addSubview:item];
    [item mas_makeConstraints:^(MASConstraintMaker *make) {
      make.width.mas_equalTo(itemWidth);
      make.height.mas_equalTo(itemHeight);
      make.left.mas_equalTo(16 + (i % 4) * itemWidth + (i % 4) * 8);
      make.top.mas_equalTo(70 + 4*((i/4 + 1)-1)  * itemHeight + 4*((i/4 + 1)-1) * 16);
    }];
  }
}

+ (void)showWithSelectedData:(NSArray<GJOilClassificationModel *> *)dataArray Block:(void (^)(GJOilClassificationModel * _Nonnull))backBlock{
  GJOilTypeSelected *selectedView = [[GJOilTypeSelected alloc]initWithFrame:CGRectZero];
  selectedView.itemClickBlcok = backBlock;
  selectedView.dataArray = dataArray;
  selectedView.frame = [UIApplication sharedApplication].delegate.window.bounds;
  [[UIApplication sharedApplication].delegate.window addSubview:selectedView];
}
@end
